package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.mapper.mwsreport.IMwsAllOrderMapper;
import com.yunchang.springboot.models.mwsreportbean.MwsAllOrder;
import com.yunchang.springboot.models.mwsreportbean.MwsAllOrderDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:51
 * @description:
 */
@Component
public class AllOrderDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Autowired
    IMwsAllOrderMapper mwsAllOrderMapper;

    /**
     * 所有订单 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area, String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i).trim(), i);
        }
        int amazonOrderIdIndex = Integer.valueOf(titleMap.get("amazon-order-id").toString());
        int merchantOrderIdIndex = Integer.valueOf(titleMap.get("merchant-order-id").toString());
        int purchaseDateIndex = Integer.valueOf(titleMap.get("purchase-date").toString());
        int lastUpdatedDateIndex = Integer.valueOf(titleMap.get("last-updated-date").toString());
        int orderStatusIndex = Integer.valueOf(titleMap.get("order-status").toString());
        int fulfillmentChannelIndex = Integer.valueOf(titleMap.get("fulfillment-channel").toString());
        int salesChannelIndex = Integer.valueOf(titleMap.get("sales-channel").toString());
        int orderChannelIndex = Integer.valueOf(titleMap.get("order-channel").toString());
        int urlIndex = Integer.valueOf(titleMap.get("url").toString());
        int shipServiceLevelIndex = Integer.valueOf(titleMap.get("ship-service-level").toString());
        int productNameIndex = Integer.valueOf(titleMap.get("product-name").toString());
        int skuIndex = Integer.valueOf(titleMap.get("sku").toString());
        int asinIndex = Integer.valueOf(titleMap.get("asin").toString());
        int itemStatusIndex = Integer.valueOf(titleMap.get("item-status").toString());
        int quantityIndex = Integer.valueOf(titleMap.get("quantity").toString());
        int currencyIndex = Integer.valueOf(titleMap.get("currency").toString());
        int itemPriceIndex = Integer.valueOf(titleMap.get("item-price").toString());
        int itemTaxIndex = Integer.valueOf(titleMap.get("item-tax").toString());
        int shippingPriceIndex = Integer.valueOf(titleMap.get("shipping-price").toString());
        int shippingTaxIndex = Integer.valueOf(titleMap.get("shipping-tax").toString());
        int giftWrapPriceIndex = Integer.valueOf(titleMap.get("gift-wrap-price").toString());
        int giftWrapTaxIndex = Integer.valueOf(titleMap.get("gift-wrap-tax").toString());
        int itemPromotionDiscountIndex = Integer.valueOf(titleMap.get("item-promotion-discount").toString());
        int shipPromotionDiscountIndex = Integer.valueOf(titleMap.get("ship-promotion-discount").toString());
        int shipStateIndex = Integer.valueOf(titleMap.get("ship-state").toString());
        int shipPostalCodeIndex = Integer.valueOf(titleMap.get("ship-postal-code").toString());
        int shipCountryIndex = Integer.valueOf(titleMap.get("ship-country").toString());
        int promotionIdsIndex = Integer.valueOf(titleMap.get("promotion-ids").toString());
        int isBusinessOrderIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("is-business-order")) {
            isBusinessOrderIndex = Integer.valueOf(titleMap.get("is-business-order").toString());
        }
        int purchaseOrderNumberIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("purchase-order-number")) {
            purchaseOrderNumberIndex = Integer.valueOf(titleMap.get("purchase-order-number").toString());
        }
        int priceDesignationIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("price-designation")) {
            priceDesignationIndex = Integer.valueOf(titleMap.get("price-designation").toString());
        }
        int customizedUrlIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("customized-url")) {
            customizedUrlIndex = Integer.valueOf(titleMap.get("customized-url").toString());
        }
        int customizedPageIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("customized-page")) {
            customizedPageIndex = Integer.valueOf(titleMap.get("customized-page").toString());
        }
        int isSoldByAbIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("is-sold-by-ab")) {
            isSoldByAbIndex = Integer.valueOf(titleMap.get("is-sold-by-ab").toString());
        }
        List<Object> insertList = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            List<String> stringList = lists.get(i);
            MwsAllOrderDO mwsAllOrder = new MwsAllOrderDO();
            mwsAllOrder.setAmazonOrderId(stringList.get(amazonOrderIdIndex));
            mwsAllOrder.setSku(stringList.get(skuIndex));
            mwsAllOrder.setAsin(stringList.get(asinIndex));
            mwsAllOrder.setLastUpdatedDate(DateUtil.ISO8601ToGMT(stringList.get(lastUpdatedDateIndex)));
            MwsAllOrderDO oneInfoByAmazonOrderIdAndSkuAndAsin = mwsAllOrderMapper.getOneInfoByAmazonOrderIdAndSkuAndAsin(
                    mwsAllOrder.getAmazonOrderId(), mwsAllOrder.getSku(), mwsAllOrder.getAsin(), area);
            //如果存在该条数据
            if (null != oneInfoByAmazonOrderIdAndSkuAndAsin && null != oneInfoByAmazonOrderIdAndSkuAndAsin.getId()) {
                //如果最后更新时间和本次获取的最后更新时间一致
                if (oneInfoByAmazonOrderIdAndSkuAndAsin.getLastUpdatedDate().equals(mwsAllOrder.getLastUpdatedDate())) {
                    continue;
                }
            }
            mwsAllOrder.setShop(shopBrandName);
            mwsAllOrder.setArea(area);
            mwsAllOrder.setMerchantOrderId(stringList.get(merchantOrderIdIndex));
            mwsAllOrder.setPurchaseDate(DateUtil.ISO8601ToGMT(stringList.get(purchaseDateIndex)));
            mwsAllOrder.setOrderStatus(stringList.get(orderStatusIndex));
            mwsAllOrder.setFulfillmentChannel(stringList.get(fulfillmentChannelIndex));
            mwsAllOrder.setSalesChannel(stringList.get(salesChannelIndex));
            mwsAllOrder.setOrderChannel(stringList.get(orderChannelIndex));
            mwsAllOrder.setUrl(stringList.get(urlIndex));
            mwsAllOrder.setShipServiceLevel(stringList.get(shipServiceLevelIndex));
            mwsAllOrder.setProductName(stringList.get(productNameIndex));
            mwsAllOrder.setItemStatus(stringList.get(itemStatusIndex));
            mwsAllOrder.setQuantity(stringList.get(quantityIndex));
            mwsAllOrder.setCurrency(stringList.get(currencyIndex));
            mwsAllOrder.setItemPrice(stringList.get(itemPriceIndex));
            mwsAllOrder.setItemTax(stringList.get(itemTaxIndex));
            mwsAllOrder.setShippingPrice(stringList.get(shippingPriceIndex));
            mwsAllOrder.setShippingTax(stringList.get(shippingTaxIndex));
            mwsAllOrder.setGiftWrapPrice(stringList.get(giftWrapPriceIndex));
            mwsAllOrder.setGiftWrapTax(stringList.get(giftWrapTaxIndex));
            mwsAllOrder.setItemPromotionDiscount(stringList.get(itemPromotionDiscountIndex));
            mwsAllOrder.setShipPromotionDiscount(stringList.get(shipPromotionDiscountIndex));
            mwsAllOrder.setShipState(stringList.get(shipStateIndex));
            mwsAllOrder.setShipPostalCode(stringList.get(shipPostalCodeIndex));
            mwsAllOrder.setShipCountry(stringList.get(shipCountryIndex));
            mwsAllOrder.setPromotionIds(stringList.get(promotionIdsIndex));
            if (isBusinessOrderIndex != WRONG_NUMBER_SIGN) {
                mwsAllOrder.setIsBusinessOrder(stringList.get(isBusinessOrderIndex));
            }

            if (purchaseOrderNumberIndex != WRONG_NUMBER_SIGN) {
                mwsAllOrder.setPurchaseOrderNumber(stringList.get(purchaseOrderNumberIndex));
            }

            if (priceDesignationIndex != WRONG_NUMBER_SIGN) {
                mwsAllOrder.setPriceDesignation(stringList.get(priceDesignationIndex));
            }

            if (customizedUrlIndex != WRONG_NUMBER_SIGN) {
                mwsAllOrder.setPriceDesignation(stringList.get(customizedUrlIndex));
            }

            if (customizedPageIndex != WRONG_NUMBER_SIGN) {
                mwsAllOrder.setPriceDesignation(stringList.get(customizedPageIndex));
            }

            if (isSoldByAbIndex != WRONG_NUMBER_SIGN) {
                mwsAllOrder.setPriceDesignation(stringList.get(isSoldByAbIndex));
            }

            mwsAllOrder.setCreateTime(DateUtil.getDate());
            mwsAllOrder.setUpdateTime(DateUtil.getDate());
            mwsAllOrder.setSellerId(sellerId);
            mwsAllOrder.setFinanceStatus(1);
            //如果没有该数据---插入数据
            if (null == oneInfoByAmazonOrderIdAndSkuAndAsin || null == oneInfoByAmazonOrderIdAndSkuAndAsin.getId()) {
                mwsAllOrder.setId(null);
            } else {
                mwsAllOrder.setId(oneInfoByAmazonOrderIdAndSkuAndAsin.getId());
            }
            insertList.add(mwsAllOrder);
        }
        return insertList;
    }
}
